package com.pabble.webnative.Interface;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.webkit.JavascriptInterface;

import com.pabble.webnative.WebActivity;

public class ClipBoard extends Base{
    private ClipboardManager clipboardManager;

    public ClipBoard(WebActivity ctx) {
        super(ctx);
    }

    private ClipboardManager init(){
        if(clipboardManager == null){
            clipboardManager = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
        }
        return clipboardManager;
    }
    @JavascriptInterface
    public String readClipBoard(){
        init();
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (clipData != null && clipData.getItemCount() > 0) {
            ClipData.Item item = clipData.getItemAt(0);

            if (item.getText() != null) {
                return item.getText().toString();
            }
        }
        return  "";
    }
    @JavascriptInterface
    public void writeClipBoard(String content){
        init();
        if(clipboardManager == null){
            clipboardManager = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
        }
        clipboardManager.setPrimaryClip(ClipData.newPlainText("text",content));
    }
}
